﻿#region License
// Copyright (c) 2009 Shengmin Zhang
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Text;
using System.Security.Cryptography;
using System.Web;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Globalization;

namespace Apprender.Utilities
{
    public static class UString
    {
        //Shengmin
        public static void GenerateRandomString(StringBuilder sb, int size)
        {
            char[] chars = new char[52];
            string str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
            chars = str.ToCharArray();           

            byte[] indices = new byte[size];
            RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
            crypto.GetNonZeroBytes(indices);
            byte index;
            int length = chars.Length;
            for (int i = 0; i < size; i++)
            {
                index = indices[i];
                sb.Append(chars[index % length]);
            }
            
        }

        //Shengmin        
        public static List<string> ToList(string str, string delimiter, int size)
        {
            str = str.Trim();
            int startIndex = 0;
            int endIndex = 0;
            int length = str.Length;
            List<string> list = new List<string>(size);
            string strSnippet = string.Empty;

            while (startIndex < length)
            {
                endIndex = str.IndexOf(delimiter, startIndex);
                if (endIndex != -1)
                {
                    strSnippet = str.Substring(startIndex, endIndex - startIndex);
                    startIndex = endIndex + 1;
                }
                else
                {
                    strSnippet = str.Substring(startIndex);
                    startIndex = length;
                }

                strSnippet = strSnippet.Trim();
                list.Add(strSnippet);
            }
            return list;
        }

        public static string DecodeUTF(string str)
        {
            string pattern = @"\\u[a-f0-9A-F]{4}";
            Regex reg = new Regex(pattern, RegexOptions.Compiled);
            MatchEvaluator evaluator = new MatchEvaluator(DecodeUTFReplaceHandler);

            return reg.Replace(str, evaluator);
        }
        private static string DecodeUTFReplaceHandler(Match m)
        {
            int i = int.Parse(m.Value.Substring(2, 4), NumberStyles.HexNumber);

            return char.ConvertFromUtf32(i);
        }

        public static object ConvertTo(string value, Type type)
        {
            TypeCode typeCode = Type.GetTypeCode(type);

            return ConvertTo(value, typeCode);
        }

        public static object ConvertTo(string value, TypeCode typeCode)
        {
            
            switch (typeCode)
            {
                case TypeCode.String:
                    return value;
                    break;
                case TypeCode.Char:
                    char ch;
                    char.TryParse(value, out ch);
                    return ch;
                    break;

                case TypeCode.Boolean:
                    bool bo;
                    bool.TryParse(value, out bo);
                    return bo;
                    break;

                case TypeCode.UInt32:
                    uint u32;
                    uint.TryParse(value, out u32);
                    return u32;
                    break;

                case TypeCode.Int32:
                    int i32;
                    int.TryParse(value, out i32);
                    return i32;
                    break;

                case TypeCode.Byte:
                    byte b;
                    byte.TryParse(value, out b);
                    return b;
                    break;

                case TypeCode.Int64:
                    long i64;
                    long.TryParse(value, out i64);
                    return i64;
                    break;

                case TypeCode.UInt64:
                    ulong u64;
                    ulong.TryParse(value, out u64);
                    return u64;
                    break;

                case TypeCode.SByte:
                    sbyte sb;
                    sbyte.TryParse(value, out sb);
                    return sb;
                    break;

                case TypeCode.Int16:
                    short i16;
                    short.TryParse(value, out i16);
                    return i16;
                    break;

                case TypeCode.UInt16:
                    ushort u16;
                    ushort.TryParse(value, out u16);
                    return u16;
                    break;

                case TypeCode.Single:
                    float single;
                    float.TryParse(value, out single);
                    return single;
                    break;

                case TypeCode.Double:
                    double d;
                    double.TryParse(value, out d);
                    return d;
                    break;               

                case TypeCode.Decimal:
                    decimal dec;
                    decimal.TryParse(value, out dec);
                    return dec;
                    break;
                
            }

            return value;
        }
    }
}
